/*******************************************************************************
 * Copyright (c) 2007, Geophysical Center RAS
 * Sergey Berezin, Dmitry Voitsekhovskiy
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms,
 * with or without modification, are permitted provided
 * that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * - Neither the name of the Geophysical Center RAS nor the names
 * of its contributors may be used to endorse or promote products
 * derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ******************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using VisualEsse.DataEngine.Types;

namespace VisualEsse.DataEngine
{
    public class VirtualParameterVector2dDataSource : DataSource, IGrid2dDataSource
    {
        private IGrid2dDataSource[] components;

        public VirtualParameterVector2dDataSource(DataProvider provider, EsseParameterDescription parameter, GeoPoint min, GeoPoint max, DateTime date)
            :
            base(provider, min, max, provider.GridStep, date)
        {
            System.Diagnostics.Debug.Assert(parameter.Dimension == 2, "Virtual Parameter should have dimension equal 2");

            components = new IGrid2dDataSource[parameter.Components.Length];
            for (int i = 0; i < parameter.Components.Length; i++)
            {
                EsseParameterDescription c = parameter.Components[i];
                components[i] = provider.CreateDataSource(c, date);
            }
        }

        #region IGrid2dDataSource Members

        public VisualEsse.DataEngine.Types.IGrid2d GetGrid()
        {
            return new UniformGrid2d(
                new UniformGrid1d((int)((lonMax - lonMin) / (lonStep)) + 1, lonMin, lonStep),
                new UniformGrid1d((int)((latMax - latMin) / (latStep)) + 1, latMin, latStep));
        }

        public new VisualEsse.DataEngine.Types.GeoRect DataDefinitionRectangle
        {
            get
            {
                return new GeoRect((float)lonMin, (float)lonMax, (float)latMin, (float)latMax);
            }
        }

        public object GetData(VisualEsse.DataEngine.Types.GeoRect region)
        {
            GeoUniformGrid2d grid;
            Vector2dArray2d data;

            GeoUniformScalarField2d fcx = components[0].GetData(region) as GeoUniformScalarField2d;
            GeoUniformScalarField2d fcy = components[1].GetData(region) as GeoUniformScalarField2d;
            if (fcx == null || fcy == null)
                throw new ApplicationException("Components of virtual parameter must be scalar field");

            grid = fcx.GeoGrid;
            data = new Vector2dArray2d(fcx.Grid.Width, fcx.Grid.Height);
            Vector2d[,] array = data.Data;

            IScalarArray2d sourceArrX = fcx.Data;
            IScalarArray2d sourceArrY = fcy.Data;
            for (int s = 0; s < array.GetLength(0); s++)
            {
                for (int t = 0; t < array.GetLength(1); t++)
                {
                    array[s, t] = new Vector2d(sourceArrX[s, t], sourceArrY[s, t]);
                }
            }
            return new GeoUniformVector2dField2d(grid, data);
        }

        #endregion

        #region IDataSource Members

        public object GetData()
        {
            GeoUniformGrid2d grid;
            Vector2dArray2d data;

            GeoUniformScalarField2d fcx = components[0].GetData() as GeoUniformScalarField2d;
            GeoUniformScalarField2d fcy = components[1].GetData() as GeoUniformScalarField2d;
            if (fcx == null || fcy == null)
                throw new ApplicationException("Components of virtual parameter must be scalar field");

            grid = fcx.GeoGrid;
            data = new Vector2dArray2d(fcx.Grid.Width, fcx.Grid.Height);
            Vector2d[,] array = data.Data;

            IScalarArray2d sourceArrX = fcx.Data;
            IScalarArray2d sourceArrY = fcy.Data;
            for (int s = 0; s < array.GetLength(0); s++)
            {
                for (int t = 0; t < array.GetLength(1); t++)
                {
                    array[s, t] = new Vector2d(sourceArrX[s, t], sourceArrY[s, t]);
                }
            }
            return new GeoUniformVector2dField2d(grid, data);
        }

        #endregion

        public override void Dispose()
        {
            foreach (IGrid2dDataSource src in components)
            {
                if (src is IDisposable)
                    ((IDisposable)src).Dispose();
            }
        }
    }
}
